﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TecBaseObjects.Objects;
using TpGlobalForms.Controller;
using TpGlobalForms.Interfaces;

namespace TpGlobalForms.Forms {

    public partial class OrganisationForm : Form, ITecForm {

        #region Members

        OrganisationController controller;

        #endregion

        #region Constructor

        public OrganisationForm(Organisation org) {
            
            InitializeComponent();
            this.controller = new OrganisationController(this);
            SetFormEventHandler();
            this.MyModel = org;
            this.MyModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.controller.ChangedProperties);
            foreach (Person p in this.MyModel.MyPersons) {
                p.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.controller.ChangedPersonsProperties);
            }
            this.controller.LoadModels();
            SetFormDataSources();
            SetFormDataBindings();
        }

        #endregion

        #region Properties

        public List<BerufstitelObject> MyBerufstitelModel {
            get;
            set;
        }

        public List<FunktionsObj> MyFunktionsModel {
            get;
            set;
        }

        public Organisation MyModel {
            get;
            set;
        }

        public List<PlzOrt> MyPlzOrtModel {
            get;
            set;
        }

        public List<RechtsformObject> MyRechtsformModel {
            get;
            set;
        }

        #endregion

        #region Methods

        private void SaveMyModel(object sender, EventArgs e) {
            this.controller.Save(this.MyModel);
        }

        public void SetFormDataSources() {
            this.bsOrganisation.DataSource = this.MyModel;
            this.bsBerufstitel.DataSource = this.MyBerufstitelModel;
            this.bsFunktion.DataSource = this.MyFunktionsModel;
            this.bsPlzOrt.DataSource = this.MyPlzOrtModel;
            this.bsRechtsform.DataSource = this.MyRechtsformModel;
        }

        private void SetFormEventHandler() {
            this.btnSpeichern.Click += new EventHandler(SaveMyModel);
            this.FormClosing += new FormClosingEventHandler(this.controller.BeforeCloseMyForm);
        }

        private void SetFormDataBindings() {
            this.prefixTextBox.DataBindings.Add(new Binding("Text", bsOrganisation, "Prefix",false, DataSourceUpdateMode.OnValidation));
            this.orgNameTextBox.DataBindings.Add(new Binding("Text", bsOrganisation, "OrgName", false, DataSourceUpdateMode.OnValidation));
            this.straßeTextBox.DataBindings.Add(new Binding("Text", bsOrganisation, "Straße", false, DataSourceUpdateMode.OnValidation));
            this.ortTextBox.DataBindings.Add(new Binding("Text", bsOrganisation, "Ort", false, DataSourceUpdateMode.OnValidation));
            //this.straßeTextBox.DataBindings.Add(new Binding("Text", bsOrganisation, "Straße"));
        }

        #endregion

        #region Events
        #endregion

    }
}
